package com.cafuc.collections;

import java.util.*;

public class CollectionOperation {
    public void listTest() {
        List<String> list1 = new ArrayList<>();
        list1.add("ddd");
        list1.add("aaa");
        list1.add("aaa");
        list1.add("bbb");
//        list1.add(null);
        list1.remove("aaa");
        list1.add(1, "eee");
        System.out.println(list1);

        List<Integer> list2 = Arrays.asList(2,1,5,4,3);
        System.out.println(list2);

        // 循环
        for (int i = 0; i < list1.size(); i ++) {
            System.out.println(list1.get(i));
        }
        System.out.println("==================");
        Iterator iterator = list1.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("==================");
        for (String item : list1) {
            System.out.println(item);
        }
        System.out.println("==================");
        list1.stream().forEach(item -> System.out.println(item));

        // 排序
        list1.sort(Comparator.naturalOrder());
        System.out.println(list1);
        list1.sort(Comparator.reverseOrder());
        System.out.println(list1);
        list1.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        Collections.sort(list1, Comparator.reverseOrder());
        System.out.println(list1);
        Collections.shuffle(list1);
        System.out.println(list1);

        // list 与 数组互转
        System.out.println(list1.toArray(new String[list1.size()]));

        System.out.println(Arrays.asList("sun", "smk", "lisa"));
    }

    public void setTest() {
        Set<String> set1 = new TreeSet<>();
        set1.add("aaa");
        set1.add("aaa");
//        set1.add(null);
        set1.add("ddd");
        set1.add("bbb");
        System.out.println(set1);

        // 循环
        for (String item : set1) {
            System.out.println(item);
        }
    }

    public void mapTest() {
        Map<String, String> map1 = new HashMap<>();
        map1.put("aaa", "sun");
        map1.put("aaa", "smk");
        map1.put("ddd", "null");
        map1.put("bbb", "bbbbb");
        System.out.println(map1);

        // 循环
        for (String key : map1.keySet()) {
            System.out.println(map1.get(key));
        }
        for (Map.Entry<String, String> entry : map1.entrySet()) {
            System.out.println(entry.getKey() + "--" + entry.getValue());
        }
    }

    public static void main(String[] args) {
        CollectionOperation co = new CollectionOperation();
//        co.listTest();
//        co.setTest();
        co.mapTest();
    }
}
